<?php

class OrderController extends Zend_Controller_Action
{

    public function init()
    {
    	
    }

    public function indexAction()
    {
    	$this->view->title = 'Website Order Secure Access';
    	$form = new Application_Form_OrderAccess();
    	$this->view->form = $form;
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		 
    		if (isset($_POST['access'])) {
    			if ($form->isValid($formData)) {
    				$this->forward('form');
    			}
    		} else {
    			$redirector = $this->_helper->getHelper('Redirector');
    			$redirector->setGoToSimple('index','index');
    		}
    	}
    }

    public function formAction()
    {
    	$request = $this->getRequest();
    	$token = $request->getParam('sec_id');
    	
    	$result = $this->_callWebService($token);
    	$this->_setNoCache();
    	$title = 'Order Your Website';
    	if ($result->status() === 'failed') {
    		$title = 'Error: No Access to Order Page';
    	}
    	$this->view->title = $title;
    	$this->view->result = $result;
    	
    	$message = null;
    	if ($result->status() === 'failed') {
    		if (!isset($token) || $result->code() === 'INVALID_TOKEN') {
    			$message = 'The secure id token supplied is invalid or missing.';
    		} else if ($result->code() === 'URL_EXPIRED') {
    			$message = 'The order form page has already expired. Please request a new link from the administrator.';
    		}
    	}
    	if (!is_null($message)) {
    		$this->view->message = $message;
    	}
    }
    
    protected function _callWebService($token)
    {
    	$config = Zend_Registry::get('config');
    	$urlValidatorService = $config->secid->validator->webservice;
    	$domainUrl = WebsiteOrderMgr_Util_Url::_siteURL();
    	$client = new Zend_Rest_Client($domainUrl . $urlValidatorService);
    	
    	$result = $client->expireUrl($token)->get();
    	
    	return $result;
    }
    
    protected function _setNoCache()
    {
    	$this->getResponse()->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
    	$this->getResponse()->setHeader('Pragma', '', true);
    	$this->getResponse()->setHeader('Expires', '', true);
    }
}



